//
//  RemoteAlarm.m
//  gdemm
//
//  Created by 乔明超 on 16/5/26.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "RemoteAlarm.h"

@implementation RemoteAlarm
static UIAlertController *alert;
static SystemSoundID soundID;
static BOOL stop;
+ (void)remoteAlarm{
    alert = [UIAlertController alertControllerWithTitle:@"您正在被远程振铃" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        stop = YES;
        AudioServicesDisposeSystemSoundID(soundID);
        
    }];
    
    [alert addAction:confirm];
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    UIViewController *rootVc = window.rootViewController;
    
    [rootVc presentViewController:alert animated:YES completion:nil];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *path = [[NSBundle mainBundle]pathForResource:@"Opening" ofType:@"m4r"];
        NSURL *url = [NSURL fileURLWithPath:path];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
        AudioServicesAddSystemSoundCompletion(soundID, nil, nil, callback, nil);
        AudioServicesPlayAlertSound(soundID);
        
    });
    
}

void callback(SystemSoundID ssID, void* __nullable clientData)
{
    if (stop) {
        AudioServicesDisposeSystemSoundID(soundID);
        stop = NO;
    }else{
    
        AudioServicesPlayAlertSound(soundID);
    }
}

@end
